package net.etfbl.projekat.verko.dao;

import java.sql.Connection;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import net.etfbl.projekat.verko.dto.Korisnik;

public class DataManager {
	
	public Connection checkOutConnection()
	{
		Connection conn = null;
		try
		{
			Context ctx= new InitialContext();
			if(ctx!=null)
			{
				Context envContext= (Context)ctx.lookup("java:/comp/env");
				if(envContext!=null)
				{
					DataSource ds= (DataSource)envContext.lookup("jdbc/mysql");
					if(ds!=null)
						conn=ds.getConnection();
					}
				}
			}
		catch(Exception ex)
		{
			System.out.println("Nisam se mogao prikljuciti na bazu: "+ex.getMessage());
		}
		return conn;
	}
	
	public void checkInConnection(Connection conn)
	{
		if(conn!=null)
		{
			try
			{
				conn.close();				
			}
			catch(SQLException ex)
			{
				
			}
		}
	}
	public int login(Korisnik korisnik){
		int status=KorisnikDAO.login(this, korisnik.getUsername(), korisnik.getPassword());
		return status;
	}
	
}